import time

import numpy as np

import MOGRO
import NSGAII
import config
import db1
import gc
# 按间距中的绿色按钮以运行脚本。
import file

if __name__ == '__main__':
    exp_data = []
    # start = time.process_time()  # 起始运行时间
    tasks = db1.init_course_task()  # taskdata存储即将进行排课的教学任务
    # 根据选课信息表中的信息将学生信息筛选出，确定学生信息
    student_list = db1.init_sel_students()  # 存储选课学生的信息
    teacher_list = db1.init_sel_teachers()
    students = [student_list[i][0] for i in range(len(student_list))]
    teachers = [teacher_list[i][0] for i in range(len(teacher_list))]
    # 存储授课老师信息
    classrooms = db1.init_classrooms()  # 教室列表信息
    # buildings = db1.init_building()  # 教学楼列表信息
    buildings_dis = db1.init_buildingDistance()  # 教学楼距离列表信息
    # np.random.seed(3)
    # random.seed(3)
    for _ in range(10):
        print(f"第{_ + 1}次。")
        start = time.time()
        results, results_fit, x, y1, y2, y3, RTlino_g = NSGAII.NSGAII(tasks, students, teachers, classrooms,
                                                                      buildings_dis)

        # 选出最优方案
        print("学生教师方差：", results_fit[1], "教室空闲率：", results_fit[2], "平均距离：", results_fit[3])
        # end = time.process_time()  # 结束运行时间
        end = time.time()  # 结束运行时间
        total_time = end - start
        print("运行时间: %.03f seconds" % total_time)  # 运行时间 : 2.999 seconds
        exp_data.append(
            [config.particals, config.MaxIter, config.step_factory, total_time, results_fit[1], results_fit[2],
             results_fit[3], round(
                config.s_t_weight * results_fit[1] + config.r_weight * results_fit[2] + config.s_t_dis_weight *
                results_fit[3], 2)])
        # file.save_scheme(results)  # 将最优解存入文件“排课结果.xlsx”中
        # Export.export_toDB(results)
        #
        # # The End time
        # Draw.fitness_line(x, y1, y2, y3)

        del results, results_fit, x, y1, y2, y3, RTlino_g, start, end
        gc.collect()

    file.save_data(exp_data)
    del exp_data, tasks, student_list, teacher_list, students, teachers, classrooms, buildings_dis, _
    gc.collect()
